package ru.spb.osll.myfit.client.widget;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.event.TagAddEvent;

import java.util.Set;

/**
 * User: Artem Bukhonov
 * Date: 20.03.12
 * Time: 17:23
 */
public class TagAddPanelWidget extends Composite {
  private Label m_tagAlreadyExistsLabel = new Label("Tag exists!");
  private final SuggestBox m_suggestBox;
  private final HorizontalPanel m_rootPanel;
  private CustomAnimation tagAlreadyExistsLabelFading = new CustomAnimation();
  private PageButtonEventListener m_listener;

  public TagAddPanelWidget() {

    MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
    Button addButton = new Button("Add tag", new ClickHandler() {
      @Override
      public void onClick(ClickEvent clickEvent) {

        if (m_listener != null && !m_suggestBox.getText().trim().equals("")) {
          TagAddEvent event = new TagAddEvent(m_suggestBox.getText());
          m_listener.actionPerformed(event);
          if (event.isAlreadyExists()) {
            showTagExistsWarning();
          }
          else {
            m_suggestBox.setText("");
          }
        }
      }
    });
    m_suggestBox = new SuggestBox(oracle);
    m_suggestBox.setAutoSelectEnabled(false);
    m_suggestBox.addKeyDownHandler(new KeyDownHandler() {
      @Override
      public void onKeyDown(KeyDownEvent event) {
        int c = event.getNativeKeyCode();
        if (c == KeyCodes.KEY_TAB) {
          if (((SuggestBox.DefaultSuggestionDisplay) m_suggestBox.getSuggestionDisplay()).isSuggestionListShowing()) {
            event.preventDefault();
            event.stopPropagation();
          }
        } else if (c == KeyCodes.KEY_ENTER) {

          if (!((SuggestBox.DefaultSuggestionDisplay) m_suggestBox.getSuggestionDisplay()).isSuggestionListShowing()) {
            if (!m_suggestBox.getText().equals("")) {
              if (m_listener != null && !m_suggestBox.getText().trim().equals("")) {
                TagAddEvent tagAddEvent = new TagAddEvent(m_suggestBox.getText());
                m_listener.actionPerformed(tagAddEvent);
                if (tagAddEvent.isAlreadyExists()) {
                  event.preventDefault();
                  event.stopPropagation();
                  showTagExistsWarning();
                }
                else {
                  m_suggestBox.setText("");
                }

              }

//              if (tagAlreadyExists(m_suggestBox.getText())) {
//                event.preventDefault();
//                event.stopPropagation();
//                showTagExistsWarning();
//              } else {
//                if (m_listener != null && !m_suggestBox.getText().equals("")) {
//                  m_listener.actionPerformed(new PageButtonEvent(PageButtonEvent.PageButtonEventType.ADD_TAG, m_suggestBox.getText()));
//                }
//                m_suggestBox.setText("");
              //}
            }
          }
        }
      }
    });

    m_rootPanel = new HorizontalPanel();
    m_rootPanel.setVerticalAlignment(m_rootPanel.ALIGN_MIDDLE);
    //HorizontalPanel addTagInnerPanel = new HorizontalPanel();
    //addTagInnerPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

    //m_rootPanel.add(addTagInnerPanel);
    //addTagInnerPanel.add(m_suggestBox);
    //addTagInnerPanel.add(addButton);
    m_rootPanel.add(m_suggestBox);
    m_rootPanel.add(addButton);
    addButton.addStyleName("addTagButtonStyle");
    m_tagAlreadyExistsLabel.addStyleName(IStyleConstants.WARNING_LABEL_STYLE);
    m_tagAlreadyExistsLabel.getElement().getStyle().setProperty("opacity", Double.toString(0));
    m_rootPanel.add(m_tagAlreadyExistsLabel);
    initWidget(m_rootPanel);
  }

  public PageButtonEventListener getListener() {
    return m_listener;
  }


  public void setListener(PageButtonEventListener listener) {
    m_listener = listener;
  }
  public class CustomAnimation extends Animation {

    @Override
    protected void onComplete() {
      super.onComplete();
      m_tagAlreadyExistsLabel.getElement().getStyle().setProperty("opacity", Double.toString(0));
    }

    @Override
    protected void onStart() {
      super.onStart();
      m_tagAlreadyExistsLabel.getElement().getStyle().setProperty("opacity", Double.toString(1));
    }
    @Override
    protected void onUpdate(double progress) {
      m_tagAlreadyExistsLabel.getElement().getStyle().setProperty("opacity", Double.toString(1 - progress));
    }

  }

  private void showTagExistsWarning() {
    tagAlreadyExistsLabelFading.run(1500);
  }

//  private boolean tagAlreadyExists(String tag) {
//    boolean exists = false;
//    int tagsCount = m_tagLabels.getWidgetCount();
//    for (int i = 0; i < tagsCount; ++i) {
//      if (m_tagLabels.getWidget(i) instanceof TagWidget) {
//        if (((TagWidget) m_tagLabels.getWidget(i)).getLabel().equals(tag)) {
//          exists = true;
//          break;
//        }
//      }
//    }
//    return exists;
//  }

  public void addSuggestions(Set<String> suggestions) {
    MultiWordSuggestOracle oracle = (MultiWordSuggestOracle)m_suggestBox.getSuggestOracle();
    for (String suggestion : suggestions) {
      oracle.add(suggestion);
    }
  }
}
